package emulator.unittest.hardware.nmos6502.operands;


import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import emulator.hardware.HwByte;
import emulator.hardware.HwWord;
import emulator.hardware.nmos6502.Cpu6502;
import emulator.hardware.nmos6502.Operand;
import emulator.hardware.nmos6502.operands.AbsoluteY;
import emulator.hardware.nmos6502.operands.OperandException;
import emulator.unittest.hardware.nmos6502.MockMemory;

public class AbsoluteYTest {

	private Cpu6502 cpu;
	private MockMemory mock_memory;
	private Operand operand;

	@Before
	public void setUp() throws Exception {
		cpu = new Cpu6502();
		mock_memory = new MockMemory();
		cpu.attach(mock_memory, null);
		cpu.setPC(0);
		cpu.setY(new HwByte(0));
		cpu.setSP(new HwByte(0x7F));
		operand = new AbsoluteY();
	}
	
	@Test
	public void testGetByteCount() throws Exception
	{
		operand.init(cpu);
		assertEquals(2, operand.getByteCount());
	}
	
	@Test
	public void testGetExtraCycles() throws Exception
	{
		operand.init(cpu);
		assertEquals(2, operand.getExtraCycles());
	}
	
	@Test
	public void testGetByte() throws Exception
	{
		cpu.setPC(0x123);
		operand.init(cpu);
		assertEquals(0x23, operand.getByte().getNumber());
	}
	
	@Test
	public void testGetByteOffset() throws Exception
	{
		cpu.setPC(0x123);
		cpu.setY(new HwByte(5));
		operand.init(cpu);
		assertEquals(0x28, operand.getByte().getNumber());
	}
	
	@Test(expected=OperandException.class)
	public void testGetWord() throws Exception
	{
		operand.init(cpu);
		operand.getWord();
	}

	@Test
	public void testGetString() throws Exception
	{
		cpu.setPC(0x123);
		operand.init(cpu);
		assertEquals("$2423,Y", operand.getString());
	}

	@Test
	public void testSetByte() throws Exception
	{
		cpu.setPC(0x123);
		operand.init(cpu);
		operand.setByte(new HwByte(0xAA));
		assertEquals(1, mock_memory.getWriteCount());
		assertEquals(0x2423, mock_memory.getLastAddress());
		assertEquals(0xAA, mock_memory.getLastData());
	}

	@Test
	public void testSetByteOffset() throws Exception
	{
		cpu.setPC(0x123);
		cpu.setY(new HwByte(5));
		operand.init(cpu);
		operand.setByte(new HwByte(0xAA));
		assertEquals(1, mock_memory.getWriteCount());
		assertEquals(0x2428, mock_memory.getLastAddress());
		assertEquals(0xAA, mock_memory.getLastData());
	}
	
	@Test(expected=OperandException.class)
	public void testSetWord() throws Exception
	{
		operand.init(cpu);
		operand.setWord(new HwWord(0));
	}
}
